<template>
  <div id="id">
    <nav-bar class="home-nav">
      <template v-slot:center>
        <div>购物街</div>
      </template>
    </nav-bar>
    <home-swiper :banners="banner" />
    <home-recommend :recommends="recommend" />
    <home-feature />
    <tab-control class="tab-control"
                 :title="['流行', '新款', '精选']"
                 @tabIndex="tabClick" />
    <goods :goodsList="showGoods" />

    <ul>
      <li></li>
      <li></li>
      <li></li>
      <li></li>
      <li></li>
      <li></li>
      <li></li>
      <li></li>
      <li></li>
      <li></li>
      <li></li>
      <li></li>
      <li></li>
      <li></li>
      <li></li>
      <li></li>
      <li></li>
      <li></li>
      <li></li>
      <li></li>
      <li></li>
      <li></li>
      <li></li>
      <li></li>
      <li></li>
      <li></li>
      <li></li>
      <li></li>
      <li></li>
      <li></li>
      <li></li>
      <li></li>
      <li></li>
      <li></li>
      <li></li>
      <li></li>
      <li></li>
      <li></li>
      <li></li>
      <li></li>
      <li></li>
      <li></li>
      <li></li>
      <li></li>
      <li></li>
      <li></li>
      <li></li>
      <li></li>
      <li></li>
      <li></li>
      <li></li>
      <li></li>
      <li></li>
      <li></li>
      <li></li>
      <li></li>
      <li></li>
      <li></li>
      <li></li>
      <li></li>
      <li></li>
      <li></li>
      <li></li>
      <li></li>
      <li></li>
      <li></li>
      <li></li>
      <li></li>
      <li></li>
      <li></li>
      <li></li>
      <li></li>
      <li></li>
      <li></li>
      <li></li>
      <li></li>
      <li></li>
      <li></li>
      <li></li>
      <li></li>
      <li></li>
      <li></li>
      <li></li>
      <li></li>
      <li></li>
      <li></li>
      <li></li>
      <li></li>
      <li></li>
      <li></li>
      <li></li>
      <li></li>
      <li></li>
      <li></li>
      <li></li>
      <li></li>
      <li></li>
      <li></li>
      <li></li>
      <li></li>
    </ul>
  </div>

</template>

<script>
import HomeSwiper from './childComps/HomeSwiper.vue'
import HomeRecommend from './childComps/HomeRecommend.vue';
import HomeFeature from './childComps/HomeFeature.vue';

import NavBar from 'components/common/navbar/NavBar.vue';
import TabControl from 'components/content/tabControl/TabControl.vue';
import Goods from 'components/content/goods/Goods.vue';

import { getHomeMultidata, getHomeGoods } from 'network/home.js';

export default {
  name: "Home",
  components: {
    HomeSwiper,
    HomeRecommend,
    HomeFeature,
    NavBar,
    TabControl,
    Goods

  },
  data () {
    return {
      banner: [],
      recommend: [],
      goods: {
        pop: { page: 0, list: [] }, //流行
        new: { page: 0, list: [] }, //新款
        sell: { page: 0, list: [] } //精选
      },
      currentType: 'pop' //pop new sell
    }
  },
  created () {
    this.getMultidata();
    this.getGoods('pop');
    this.getGoods('new');
    this.getGoods('sell');
  },
  computed: {
    //展示商品数据
    showGoods () {
      return this.goods[this.currentType].list;
    }
  },
  methods: {
    /**
     * 事件监听相关的方法
     */

    //TabControl组件传过来index
    tabClick (index) {
      //console.log(index);
      //根据当前的索引值修改当前所处的类型pop new sell
      switch (index) {
        case 0:
          this.currentType = 'pop';
          break;
        case 1:
          this.currentType = 'new';
          break;
        case 2:
          this.currentType = 'sell';
          break;
      }
    },



    /**
     * 网络请求相关的方法
     */

    //获得首页的数据
    async getMultidata () {
      const res = await getHomeMultidata();
      const { banner, recommend } = res.data;
      this.banner = banner.list;
      this.recommend = recommend.list;
    },

    //得到商品数据
    async getGoods (type) {
      const page = this.goods[type].page + 1;
      const res = await getHomeGoods(type, page);
      this.goods[type].list.push(...res.data.list);
      this.goods[type].page = page;
    }
  },


}
</script>

<style scoped>
.home-nav {
  background-color: var(--color-tint);
  color: #fff;
  position: fixed;
  left: 0;
  right: 0;
  top: 0;
  z-index: 10;
}

.tab-control {
  position: sticky;
  top: 44px;
  background-color: #fff;
}
</style>